﻿Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.Data

Partial Class Purchase_LPODtl
    Inherits System.Web.UI.Page

    Dim _CommandName As String
    Dim _Command As String
    Dim Grid As RadGrid
    Dim LPOHdrObj As New DBLPOHdrSP
    Dim LPOHdrDS As New Data.DataSet
    Dim LPODtlObj As New DBLPODtlSP
    Dim LPODtlDS As New Data.DataSet
    Dim ItemObj As New DBItemSP
    Dim ItemDataSet As New DataSet
    Dim UnitObj As New DBUnitSP
    Dim UnitDataSet As New DataSet

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            hdnLPOID.Value = Request.QueryString("ID").ToString

            If hdnLPOID.Value <> "" And hdnLPOID.Value <> "0" And hdnLPOID.Value <> "-1" Then
                FillLPOHdr()
                'Edit Mode
                rgLPODtl.DataSourceID = "sdsLPODtl"
                rgLPODtl.DataBind()
            Else
                hdnLPOID.Value = "-1"
                rdpLPODate.DbSelectedDate = DateTime.Now()
                rbtnDelete.Visible = False
            End If
            btnCancel.Attributes.Add("onClick", "javascript:history.back(); return false;")
        End If
    End Sub

    Protected Sub rgLPODtl_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgLPODtl.ItemCommand
        _CommandName = e.CommandName
    End Sub

    Protected Sub rcbPurReqNo_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        rgLPODtl.DataSourceID = "sdsPurchaseRequestDtl"
        rgLPODtl.DataBind()
    End Sub

    Protected Sub rgLPODtl_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgLPODtl.ItemDataBound
        If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) And _CommandName <> "InitInsert" Then
            'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then

            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            Dim parentItem As GridDataItem = editFormItem.ParentItem

            Dim box As RadComboBox = CType(editFormItem.FindControl("WUCItem").FindControl("rcbv"), RadComboBox)
            box.SelectedValue = parentItem("Item").Text
            Dim box1 As RadComboBox = CType(editFormItem.FindControl("WUCItem").FindControl("rcbd"), RadComboBox)
            box1.SelectedValue = parentItem("Item").Text

            Dim box2 As RadComboBox = CType(editFormItem.FindControl("WUCUnit").FindControl("rcbv"), RadComboBox)
            box2.SelectedValue = parentItem("Unit").Text
            Dim box3 As RadComboBox = CType(editFormItem.FindControl("WUCUnit").FindControl("rcbd"), RadComboBox)
            box3.SelectedValue = parentItem("Unit").Text

        End If

    End Sub



    Protected Sub WUCItem_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
        Dim activeEditItem As Integer = 0
        For i As Integer = 0 To rgLPODtl.Items.Count - 1
            If rgLPODtl.Items(i).Edit Then
                activeEditItem = i
                Exit For
            End If
        Next

        Dim editFormItem As GridEditFormItem = DirectCast(rgLPODtl.MasterTableView.GetItems(GridItemType.EditFormItem)(activeEditItem), GridEditFormItem)
        odsDynamicUnit.SelectParameters("UnitID").DefaultValue = e.Value
        CType(editFormItem.FindControl("WUCUnit").FindControl("rcbv"), RadComboBox).DataBind()
    End Sub

    Sub SaveControls()
        hdnLPOID.Value = LPOHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteScalar, "SaveLPOHdr", _
                                                      hdnLPOID.Value, rtbYear.Text, rdpEntryDate.DbSelectedDate, rtbLPONo.Text, rdpLPODate.DbSelectedDate, _
                                                      rcbCostCenter.SelectedValue, rcbMainCostCenter.SelectedValue, rcbPurReqNo.SelectedValue, rtbDeliveryPlace.Text, rcbSupplier.SelectedValue, rtbImportPeriod.Text, rtbRemarks.Text)
        SaveDtl()
    End Sub

    Protected Sub SaveDtl()

        LPODtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteLPODtlByLPOID", , hdnLPOID.Value)

        For counter = 0 To rgLPODtl.Items.Count - 1
            LPODtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "SaveLPODtl", rgLPODtl.Items(counter)("ID").Text, _
            hdnLPOID.Value, _
            IIf(rgLPODtl.Items(counter)("ProgramNo").Text = "&nbsp;", "", rgLPODtl.Items(counter)("ProgramNo").Text), _
            IIf(rgLPODtl.Items(counter)("ProjectNo").Text = "&nbsp;", "", rgLPODtl.Items(counter)("ProjectNo").Text), _
             IIf(rgLPODtl.Items(counter)("Item").Text = "&nbsp;", "", rgLPODtl.Items(counter)("Item").Text), _
             IIf(rgLPODtl.Items(counter)("Unit").Text = "&nbsp;", "", rgLPODtl.Items(counter)("Unit").Text), _
             IIf(rgLPODtl.Items(counter)("UnitPrice").Text = "&nbsp;", "", rgLPODtl.Items(counter)("UnitPrice").Text), _
             IIf(rgLPODtl.Items(counter)("Qty").Text = "&nbsp;", "", rgLPODtl.Items(counter)("Qty").Text), _
             IIf(rgLPODtl.Items(counter)("PurchaseReason").Text = "&nbsp;", "", rgLPODtl.Items(counter)("PurchaseReason").Text), _
             IIf(rgLPODtl.Items(counter)("Remarks").Text = "&nbsp;", "", rgLPODtl.Items(counter)("Remarks").Text))
        Next
    End Sub

    Protected Sub FillLPOHdr()
        LPOHdrDS = LPOHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvLPOHdrByID", hdnLPOID.Value)
        If Not LPOHdrDS Is Nothing Then
            If LPOHdrDS.Tables.Count > 0 Then
                If LPOHdrDS.Tables(0).Rows.Count > 0 Then

                    rtbYear.Text = LPOHdrDS.Tables(0).Rows(0).Item("Year").ToString()
                    rtbLPONo.Text = LPOHdrDS.Tables(0).Rows(0).Item("LPONo").ToString()
                    rdpLPODate.DbSelectedDate = LPOHdrDS.Tables(0).Rows(0).Item("LPODate").ToString()
                    rdpEntryDate.DbSelectedDate = LPOHdrDS.Tables(0).Rows(0).Item("EntryDate").ToString()
                    rcbCostCenter.DataBind()
                    rcbCostCenter.SelectedValue = LPOHdrDS.Tables(0).Rows(0).Item("CostCenter").ToString()
                    rcbMainCostCenter.DataBind()
                    rcbMainCostCenter.SelectedValue = LPOHdrDS.Tables(0).Rows(0).Item("MainCostCenter").ToString()
                    rcbPurReqNo.DataBind()
                    rcbPurReqNo.SelectedValue = LPOHdrDS.Tables(0).Rows(0).Item("PurReqID").ToString()
                    rcbSupplier.DataBind()
                    rcbSupplier.SelectedValue = LPOHdrDS.Tables(0).Rows(0).Item("SupplierID").ToString()
                    rtbDeliveryPlace.Text = LPOHdrDS.Tables(0).Rows(0).Item("DeliveryPlace").ToString()
                    rtbImportPeriod.Text = LPOHdrDS.Tables(0).Rows(0).Item("ImportPeriod").ToString()
                    rtbRemarks.Text = LPOHdrDS.Tables(0).Rows(0).Item("Remarks").ToString()
                End If
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            SaveControls()
            RadAjaxManager1.ResponseScripts.Add("radalert('Information Saved Successfully !', 270, 100);")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Protected Sub rbtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtnDelete.Click
        DeleteRecord()
    End Sub
    Sub DeleteRecord()

        Try
            LPODtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteLPODtlByLPOID", , hdnLPOID.Value)
            LPOHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteLPOHdr", hdnLPOID.Value)
            Response.Redirect("~/Purchase/LPOHdr.aspx")

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally

        End Try
    End Sub

End Class
